<template>
  <div class="content">
    <div class="cloud-header">
      <h2>卡券功能</h2>
    </div>
    <div class="cloud-body">
      <div class="cloud-subset-title">
        <div class="cloud-subset-action">
          <Button color="primary" @click="addCard">+创建卡券</Button>
        </div>
        <p class="cloud-subset-tips">在开发的场景同，通常一个项目管理多个应用，如微信公众号，微信小程序，数据后台等等公用的一个数据库，此处用于区分不同应用场景<br></p>
      </div>
      <div class="cloud-rules-body">
        <Table :datas="datas" stripe :loading="loading">
          <TableItem title="类型">
            <template slot-scope="{data}">
              {{available_type(data.type)}}
            </template>
          </TableItem>
          <TableItem title="创建时间">
            <template slot-scope="{data}">
              {{data.creation_time}}
            </template>
          </TableItem>
          <TableItem title="卡券名称">
            <template slot-scope="{data}">
              {{data.brand_name}}
            </template>
          </TableItem>
          <TableItem title="卡券标题" align="center">
            <template slot-scope="{data}">
              {{data.title}}
            </template>
          </TableItem>
          <TableItem title="审核状态" align="center">
            <template slot-scope="{data}">
              {{data.card_id?'已审核':'待审核'}}
            </template>
          </TableItem>
          <TableItem title="上架状态" align="center">
            <template slot-scope="{data}">
              {{data.putaway === '1'?'已上架':'已下架'}}
            </template>
          </TableItem>
          <TableItem title="库存" prop="quantity" align="center"></TableItem>
          <TableItem title="操作">
            <template slot-scope="{data}">
              <button class="h-btn h-btn-no-border" @click="onUpdate(data._id)">查看</button>
              <Poptip v-if="Number(data.putaway)===1 && data.card_id" content="确认下架?" @confirm="onPutaway(data,0)">
                <button class="h-btn h-btn-no-border">下架</button>
              </Poptip>
              <Poptip v-if="Number(data.putaway)===0 && data.card_id" content="确认上架?" @confirm="onPutaway(data,1)">
                <button class="h-btn h-btn-no-border">上架</button>
              </Poptip>
              <!--               <Poptip content="确认删除?" @confirm="onDelete(datas,data)">
                <button class="h-btn h-btn-no-border">删除</button>
              </Poptip> -->
            </template>
          </TableItem>
          <div slot="empty">自定义提醒：暂时无数据</div>
        </Table>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
export default {
  data() {
    return {
      loading: false,
      datas: []
    };
  },
  /**
   * 页面加载执行
   * @return {[type]} [description]
   */
  mounted() {
    this.initialize();
  },
  methods: {
    available_putaway(value) {
      value = Number(value);
      switch (value) {
        case 0:
          return '下架';
        case 1:
          return '投放成功';
      }
    },
    available_state(value) {
      switch (value) {
        case 'CARD_STATUS_NOT_VERIFY':
          return '待审核';
        case 'CARD_STATUS_VERIFY_FAIL':
          return '审核中';
        case 'CARD_STATUS_VERIFY_OK':
          return '审核通过';
        case 'CARD_STATUS_DELETE':
          return '已删除';
        case 'CARD_STATUS_DISPATCH':
          return '已投放';
      }
    },
    available_type(value) {
      switch (value) {
        case 'DISCOUNT':
          return '折扣券';
        case 'GIFT':
          return '兑换券';
        case 'CASH':
          return '代金券';
      }
    },
    onUpdate(id) {
      this.$router.push({
        name: 'addcoupon',
        params: {
          id: id
        }
      });
    },
    addCard() {
      this.$router.push('/ticket/addcoupon');
    },
    /**
     * 卡券下架
     * @return {[type]} [description]
     */
    onPutaway(data, status = 0) {
      this.$cloud.name('card').where({ _id: data._id }).update({
        putaway: status.toString()
      }).then(res => {
        data.putaway = status;
        this.$forceUpdate();
      }).catch(error => {
        log(error);
      });
    },
    /**
     * 删除卡券
     * @param  {[type]} id [description]
     * @return {[type]}    [description]
     */
    onDelete(datas, data) {
      this.$cloud.name('card').where({ _id: data._id }).delete().then(res => {
        datas.splice(datas.indexOf(data), 1);
        this.$forceUpdate();
      }).catch(error => {
        log(error);
      });
    },
    initialize() {
      let _uniqueid = this.$cloud.userInfo()._uniqueid;
      this.loading = true;
      this.$cloud.name('card').where({deleted:"1",_uniqueid}).limit(-1).select().then(res => {
        this.datas = res;
        this.loading = false;
      }).catch(error => {
        log(error);
        this.loading = false;
      });
    }
  }
};

</script>
<style lang="less" scoped="true">
.cloud-rules-body {
  margin: 0 15px;
}

</style>
